<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('array_remove'))
{
	// 从数组里移取一个键值为$key的项，并返回此KEY的值 
	function array_remove(&$array,$key)
	{
		$arr = array();$value = null;
		foreach($array as $k=>$v)
		{
			if($k==$key)
			{
				$value = $v;
			}
			else
			{
				$arr[$k] = $v; 
			}
		}
		$array = $arr;
		return $value;
	}
}

if ( ! function_exists('p_r'))
{
	function p_r($obj)
	{
		if(is_array($obj))
		{
			$new_obj = array();
			foreach($obj as $line)
			{
				$new_line = null;
				foreach ($line as $key=>$value)
				{
					if($key!="ci" && $key!="db" && $key!="load" && $key!="self")
					{
						$new_line->$key = $value;
					}
				}
				$new_obj[] = $new_line;
			}
			print_r($new_obj);
		}
		else 
		{
			$new_obj = null;
			foreach ($obj as $key=>$value)
			{
				if($key!="ci" && $key!="db" && $key!="load"  && $key!="self")
				{
					$new_obj->$key = $value;
				}
			}
			print_r($new_obj);
		}
	}
}

if(!function_exists("guid"))
{
	function guid() {
		$charid = strtoupper(md5(uniqid(mt_rand(), true)));
		$hyphen = chr(45);// "-"
		//$uuid = chr(123)// "{"
		$uuid = ""
		.substr($charid, 0, 8).$hyphen
		.substr($charid,12, 4).$hyphen
		.substr($charid,16, 4).$hyphen
		.substr($charid,20,12); 
		//.chr(125);// "}"
		return $uuid;
	}
}

if(!function_exists("_escape")) 
{
	function _escape($str)
	{
		return addcslashes($str, "\v\t\n\r\f\"\\/"); 
	}
}

function my_json_encode($in) {

	$out = "";
	if (is_object($in)) {
		$class_vars = get_object_vars(($in));
		$arr = array();
		foreach ($class_vars as $key => $val) {
			// $arr[$key] = "\"{$_escape($key)}\":\"{$val}\"";
			$arr[$key] = "\""._escape($key)."\":\"$val\"";
		}

		// print_r($arr);exit;
		$val = implode(',', $arr);
		$out .= "{{$val}}";
	}elseif (is_array($in)) {
		// echo "abc";exit;
		$obj = false;
		$arr = array();
		foreach($in AS $key => $val) {
			if(!is_numeric($key)) {
				$obj = true;
			}
			$arr[$key] = my_json_encode($val);
		}
		if($obj) {
			foreach($arr AS $key => $val) {
				// $arr[$key] = "\"{$_escape($key)}\":{$val}";
				$arr[$key] = "\""._escape($key)."\":$val";
			}

			// print_r($arr);exit;
			$val = implode(',', $arr);
			$out .= "{{$val}}";
		}else {
			$val = implode(',', $arr);
			$out .= "[{$val}]";
		}
	}elseif (is_bool($in)) {
		$out .= $in ? 'true' : 'false';
	}elseif (is_null($in)) {
		$out .= 'null';
	}elseif (is_string($in)) {
		// $out .= "\"{$_escape($in)}\"";
		$out .= "\""._escape($in)."\"";
	}else { 
		$out .= $in;
	}

	//     echo "{$out}";exit;
	return "{$out}";
}

/*  
function my_json_encode($in) { 
	$_escape = function ($str) {
		return addcslashes($str, "\v\t\n\r\f\"\\/");
	}; 
	$out = "";
	if (is_object($in)) {
		$class_vars = get_object_vars(($in));
		$arr = array();
		foreach ($class_vars as $key => $val) {
			$arr[$key] = "\"{$_escape($key)}\":\"{$val}\"";
			// $arr[$key] = "\""._escape($key)."\":\"$val\"";
		}
		
		// print_r($arr);exit; 
		$val = implode(',', $arr);
		$out .= "{{$val}}";
	}elseif (is_array($in)) {
		// echo "abc";exit;
		$obj = false;
		$arr = array();
		foreach($in AS $key => $val) {
			if(!is_numeric($key)) {
				$obj = true;
			}
			$arr[$key] = my_json_encode($val);
		}
		if($obj) {
			foreach($arr AS $key => $val) {
				$arr[$key] = "\"{$_escape($key)}\":{$val}"; 
				// $arr[$key] = "\""._escape($key)."\":\"$val}\"";
			}
			
			// print_r($arr);exit; 
			$val = implode(',', $arr);
			$out .= "{{$val}}";
		}else {
			$val = implode(',', $arr);
			$out .= "[{$val}]";
		}
	}elseif (is_bool($in)) {
		$out .= $in ? 'true' : 'false';
	}elseif (is_null($in)) {
		$out .= 'null';
	}elseif (is_string($in)) {
		$out .= "\"{$_escape($in)}\"";
		// $out .= "\""._escape($in)."\"";
	}else {
		$out .= $in;
	}
	
	//     echo "{$out}";exit; 
	return "{$out}";
} 
*/ 